<template>
	<view>
		
		<qiun-title-bar title="博客类别比例" ></qiun-title-bar>
		<view class="charts-box">
			<qiun-data-charts type="pie" :opts="{legend:{float:'left',fontSize:12}}" :chartData="pie" ></qiun-data-charts>	
		</view>
		<qiun-title-bar title="最新博客分析" />
		<view class="charts-box">
			<qiun-data-charts type="column" canvasId="scrollcolumnid"
				:opts="{enableScroll:true,xAxis:{scrollShow:true,itemCount:4,disableGrid:true, fontSize:10}}" :ontouch="true"
				:canvas2d="true" :chartData="column" ></qiun-data-charts>
		</view>
		<qiun-title-bar title="常用标签词云" ></qiun-title-bar>
		<view class="charts-box">
		<qiun-data-charts type="word" :chartData="word"></qiun-data-charts>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pie: {},
				column: {},
				word:{},
			}
		},
		onLoad() {
			this.getCategories()
			this.getTopBlogs()
			this.getTags()
		},
		methods: {
			getCategories() {
				this.pie = {
					series: [{
						data: []
					}]
				}
				let url = this.$params.host + this.$params.action_categories
				this.$req.get(url, res => {
					url = this.$params.host + this.$params.action_category_count
					res.data.data.forEach(async (c) => {
						// 发送请求
						let r = await this.$req.requestAsync(url + c.id)
						if (r.success && r.data > 0) {
							let item = {
								name: c.name,
								value: r.data
							}
							this.pie.series[0].data.push(item)
						}
					})
				})
			},
			getTopBlogs() {
				this.column = {
					categories: [],
					series: [{
						name: "阅读量",
						data: []
					}, {
						name: "评论数量",
						data: []
					}]
				}
				let url = this.$params.host + this.$params.action_top_blogs
				this.$req.get(url, res => {
					res.data.data.forEach(b => {
						if(b.title.length>10){
							b.title=b.title.substr(0,8)+"..."
						}
						this.column.categories.push(b.title)
						this.column.series[0].data.push(b.readCount)
						this.column.series[1].data.push(b.comments.length)
					})
				})
			},
			getTags(){
				this.word={
					series: []
				}
				let url=this.$params.host+this.$params.action_tags
				this.$req.get(url,res=>{
					url=this.$params.host+this.$params.action_tag_count
					res.data.data.forEach(async(c)=>{
						let r=await this.$req.requestAsync(url+c.id)
						if(r.success&&r.data>0){
							let item={
								name:c.name,
								textSize:parseInt(r.data)*10
							}
							this.word.series.push(item)
						}
					})
				})
			}
		}
	}
</script>

<style>
	@import url(charts.css);
</style>